﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 搭建EFCore开发环境
{
    public class BookEntityConfig : IEntityTypeConfiguration<Book2>
    {
        public void Configure(EntityTypeBuilder<Book2> builder)
        {
            builder.ToTable("T_Books2");

            //该行的作用：创建表的时候，忽略Price属性，不予数据库建立联系
            builder.Ignore(e => e.Price);

            //配置列名
            builder.Property(e=>e.Author).HasColumnName("AuthorName");

            //指定数据类型
            builder.Property(e => e.Author).HasColumnType("nvarchar(40)");

            //指定其他列作为主键
            //builder.HasKey(e=>e.Author);

            //设置默认值
            builder.Property(e => e.Price).HasDefaultValue(88.9);

            //添加索引
            builder.HasIndex(e=>e.Price);

            //添加复合索引
            //builder.HasIndex(e => new { e.Id, e.Title });

            //唯一索引
            builder.HasIndex(e => e.Author).IsUnique();
        }
    }
}
